package com.ruoyi.journal.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.journal.domain.BfxAccount;
import com.ruoyi.journal.domain.BfxCategories;
import com.ruoyi.journal.mapper.BfxAccountMapper;
import com.ruoyi.journal.mapper.BfxCategoriesMapper;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.journal.mapper.BfxCashJournalMapper;
import com.ruoyi.journal.domain.BfxCashJournal;
import com.ruoyi.journal.service.IBfxCashJournalService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 现金日记账Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-25
 */
@Service
public class BfxCashJournalServiceImpl implements IBfxCashJournalService 
{
    @Autowired
    private BfxCashJournalMapper bfxCashJournalMapper;

    @Autowired
    private BfxAccountMapper bfxAccountMapper;

    @Autowired
    private BfxCategoriesMapper bfxCategoriesMapper;


    /**
     * 查询现金日记账
     * 
     * @param id 现金日记账主键
     * @return 现金日记账
     */
    @Override
    public BfxCashJournal selectBfxCashJournalById(Long id)
    {
        return bfxCashJournalMapper.selectBfxCashJournalById(id);
    }

    /**
     * 查询现金日记账列表
     * 
     * @param bfxCashJournal 现金日记账
     * @return 现金日记账
     */
    @Override
    public List<BfxCashJournal> selectBfxCashJournalList(BfxCashJournal bfxCashJournal)
    {
        return bfxCashJournalMapper.selectBfxCashJournalList(bfxCashJournal);
    }

    private int a = 0;

    public void increment() {
        a = a + 1;  // 自增操作
    }

    public int getA() {
        return a;
    }

    /**
     * 新增现金日记账
     * 
     * @param bfxCashJournal 现金日记账
     * @return 结果
     */
    @Override
    public int insertBfxCashJournal(BfxCashJournal bfxCashJournal)
    {

        //判断摘要是否填写
        if (bfxCashJournal.getAbstractMsg()==null){
            throw new RuntimeException("摘要不能为空");
        }
        //判断收入类别是否填写
        if (bfxCashJournal.getRevenueId()==null){
            throw new RuntimeException("收入类别必须填写");
        }
        //判断支出类别是否填写
        if(bfxCashJournal.getContactUnitsId()==null){
            throw new RuntimeException("往来单位必须填写");
        }
        // 检查是否未填写任何金额
        if (bfxCashJournal.getDebitAmount() == null && bfxCashJournal.getExpensesAmount() == null) {
            throw new IllegalArgumentException("必须填写收入或支出");
        }
        // 检查是否同时填写了收入和支出
        if (bfxCashJournal.getDebitAmount() != null && bfxCashJournal.getExpensesAmount() != null) {
            throw new IllegalArgumentException("禁止同时添加");
        }

        Integer accountId = bfxCashJournal.getAccountId();
        System.out.println(accountId);
        BfxAccount bfxAccount = bfxAccountMapper.selectBfxAccountById(Long.valueOf(accountId));
        System.out.println(bfxAccount);
        //如果选择的是收入
        if (bfxCashJournal.getRevenueId()==1){
            // 检查是否同时填写了支出和收入
            if (bfxCashJournal.getExpensesAmount() != null) {
                throw new IllegalArgumentException("请根据所选填入收入支出");
            }
            BigDecimal balance = new BigDecimal(String.valueOf(bfxAccount.getAccountPrice()));
            BigDecimal balance1 = new BigDecimal(String.valueOf(bfxCashJournal.getDebitAmount()));
            bfxAccount.setAccountPrice(balance.add(balance1));
            bfxAccountMapper.updateBfxAccount(bfxAccount);
        } else if (bfxCashJournal.getRevenueId()==2) {
            // 检查是否同时填写了支出和收入
            if (bfxCashJournal.getDebitAmount() != null) {
                throw new IllegalArgumentException("请根据所选填入收入支出");
            }
            //初始化数据
            BigDecimal balance = new BigDecimal(String.valueOf(bfxAccount.getAccountPrice()));
            BigDecimal balance1 = new BigDecimal(String.valueOf(bfxCashJournal.getExpensesAmount()));
            //输入的支出不能超过账户余额
            if(balance1.compareTo(balance) > 0){
                throw new RuntimeException("支出金额不能超过账户余额");
            }
            //如果支出小于余额，可以支出
            bfxAccount.setAccountPrice(balance.subtract(balance1));
            bfxAccountMapper.updateBfxAccount(bfxAccount);
        }

        //收入支出后初始化余额
        bfxCashJournal.setBalance(bfxAccount.getAccountPrice());
        //日期是当前日期
        bfxCashJournal.setCreateAt(new Date());
        //规定流水号格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String datePart = dateFormat.format(new Date());
        Random random = new Random();
        int randomNumber = random.nextInt(900000000) + 100000000; // 保证是9位
        bfxCashJournal.setTransactionSerialNumber(datePart+randomNumber);
        //调用一次就自增一次，保证日记账序号的不重复性
        BfxCashJournalServiceImpl example = new BfxCashJournalServiceImpl();
        example.increment();
        System.out.println(example.getA());  // 输出: 1
        int a1 = example.getA();
        System.out.println(a1);
        int i = RandomUtils.nextInt(1, 1000);
        System.out.println(i);
        //规定日记账序号格式
        bfxCashJournal.setJournalNumber(i+"-"+"00"+i+"-"+datePart+"-"+i);
        //实现添加
        return bfxCashJournalMapper.insertBfxCashJournal(bfxCashJournal);
    }




    /**
     * 修改现金日记账
     * 
     * @param bfxCashJournal 现金日记账
     * @return 结果
     */
    @Override
    public int updateBfxCashJournal(BfxCashJournal bfxCashJournal)
    {
        Integer accountId1 = bfxCashJournal.getAccountId();
        BfxAccount bfxAccount = bfxAccountMapper.selectBfxAccountById(Long.valueOf(accountId1));
        if (bfxCashJournal.getRevenueId()==1){
            // 检查是否同时填写了支出和收入
            if (bfxCashJournal.getExpensesAmount() != null) {
                throw new IllegalArgumentException("请根据所选填入收入支出");
            }
            BigDecimal balance = new BigDecimal(String.valueOf(bfxAccount.getAccountPrice()));
            BigDecimal balance1 = new BigDecimal(String.valueOf(bfxCashJournal.getDebitAmount()));
            bfxAccount.setAccountPrice(balance.add(balance1));
        } else if (bfxCashJournal.getRevenueId()==2) {
            // 检查是否同时填写了支出和收入
            if (bfxCashJournal.getDebitAmount() != null) {
                throw new IllegalArgumentException("请根据所选填入收入支出");
            }
            //初始化数据
            BigDecimal balance = new BigDecimal(String.valueOf(bfxAccount.getAccountPrice()));
            BigDecimal balance1 = new BigDecimal(String.valueOf(bfxCashJournal.getExpensesAmount()));
            //输入的支出不能超过账户余额
            if(balance1.compareTo(balance) > 0){
                throw new RuntimeException("支出金额不能超过账户余额");
            }
            //如果支出小于余额，可以支出
            bfxAccount.setAccountPrice(balance.subtract(balance1));
        }
        bfxAccountMapper.updateBfxAccount(bfxAccount);
        bfxCashJournal.setBalance(bfxAccount.getAccountPrice());
        if (bfxCashJournal.getDebitAmount()!=null){
            bfxCashJournal.setExpensesAmount(0L);
        }else if (bfxCashJournal.getExpensesAmount()!=null){
            bfxCashJournal.setDebitAmount(0L);
        }

        System.out.println(bfxCashJournal);
        return bfxCashJournalMapper.updateBfxCashJournal(bfxCashJournal);
    }

    /**
     * 批量删除现金日记账
     * 
     * @param ids 需要删除的现金日记账主键
     * @return 结果
     */
    @Override
    public int deleteBfxCashJournalByIds(Long[] ids)
    {
        return bfxCashJournalMapper.deleteBfxCashJournalByIds(ids);
    }

    /**
     * 删除现金日记账信息
     * 
     * @param id 现金日记账主键
     * @return 结果
     */
    @Override
    public int deleteBfxCashJournalById(Long id)
    {
        return bfxCashJournalMapper.deleteBfxCashJournalById(id);
    }

}
